package com.zmovie.admin.controller.cinema;

import cn.hutool.core.util.StrUtil;
import com.zmovie.admin.constant.Constants;
import com.zmovie.common.error.BusinessException;
import com.zmovie.common.error.EnumBusinessError;
import com.zmovie.common.response.CommonReturnType;
import com.zmovie.framework.domain.CinemaBrand;
import com.zmovie.framework.service.CinemaBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec 电影院品牌模块
 * @since 2023-11-30 12:00
 */
@Controller
@RequestMapping("/cinema/brand")
@Api(tags = "电影院品牌模块")
public class CinemaBrandController {
    @Autowired
    private CinemaBrandService cinemaBrandService;
    /********品牌********/
    @GetMapping
    @ApiOperation("电影院品牌页面")
    public String index(Model model){
        //查询全部
        model.addAttribute(Constants.CINEMA_BRANDS_KEY, cinemaBrandService.list());
        return "cinema/cinema_brand";
    }
    @GetMapping("/{id}")
    @ApiOperation("查询品牌信息")
    @ResponseBody
    public Object getBrand(@PathVariable("id") Integer id){
        return CommonReturnType.success(cinemaBrandService.getById(id));
    }

    @PostMapping("/search")
    @ApiOperation("条件查询品牌")
    public Object searchBrand(CinemaBrand cinemaBrand, Model model){
        List<CinemaBrand> cinemaBrands = cinemaBrandService.listCinemaBrands(cinemaBrand);
        model.addAttribute(Constants.CINEMA_BRANDS_KEY, cinemaBrands);
        //返回渲染后的表格
        return "cinema/cinema_brand :: brandList";
    }

    @PostMapping
    @ApiOperation("添加影院品牌")
    @ResponseBody
    public Object addBrand(CinemaBrand cinemaBrand) throws BusinessException {
        //参数校验
        if(StrUtil.isEmpty(cinemaBrand.getName())){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        try {
            boolean save = cinemaBrandService.save(cinemaBrand);
            if(!save){
                //添加失败
                return CommonReturnType.fail("添加失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            //SQL异常：名称存在
            throw new BusinessException(EnumBusinessError.NAME_EXISTS);
        }
        return CommonReturnType.success("添加成功!");
    }
    @PutMapping
    @ApiOperation("更新影院品牌")
    @ResponseBody
    public Object updateBrand(CinemaBrand cinemaBrand) throws BusinessException {
        //参数校验
        if(cinemaBrand.getId() == null || StrUtil.isEmpty(cinemaBrand.getName())){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        //更新
        cinemaBrandService.updateById(cinemaBrand);
        return CommonReturnType.success("修改成功!");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除影院品牌")
    @ResponseBody
    public Object deleteBrand(@PathVariable("id") Integer id){
        cinemaBrandService.removeById(id);
        return CommonReturnType.success("删除成功！");
    }
    @DeleteMapping
    @ApiOperation("批量删除影院品牌")
    @ResponseBody
    public Object deleteBatch(Integer[] id) throws BusinessException {
        //参数校验
        if(id == null || id.length == 0){
            throw new BusinessException(EnumBusinessError.PARAMETER_VALIDATION_ERROR);
        }
        //批量删除
        cinemaBrandService.removeByIds(Arrays.asList(id));
        return CommonReturnType.success("批量删除成功！");
    }
}
